<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>编辑-配偶、父母或其他信息</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />

    <style>
        .discell {
            text-align:right;
            width:100px;
        }
        .input {
            width:100px;
        }
    </style>
</head>
<body>
<div class="mini-fit">
    <div style="height:100%;width: 100%;overflow-x: auto;overflow-y: auto;">
        <form id="form1" method="post">
            <input name="id" id="id" class="mini-hidden" style="width: 100%" readonly />
            <input name="priNum" id="priNum" class="mini-hidden" style="width: 100%" readonly />
            <br>
            <table align="center" style="border-collapse:separate; border-spacing:10px;">

                <tr>
                    <td class="discell"><font color="#dc143c">*</font>姓&emsp;&emsp;名:</td>
                    <td class="input">
                        <input name="thename" id="thename" class="mini-textbox" required="true" emptyText="请输入姓名" style="width: 230px" />
                    </td>
                    <td class="discell">关&emsp;&emsp;系:</td>
                    <td class="input">
                        <input name="relation" id="relation" class="mini-combobox"
                               textField="codeName" valueField="codeId" emptyText="请选择..." style="width: 230px;"
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_6&pcodeId=99&sn=1"
                        />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>证件类型:</td>
                    <td class="input">
                        <input name="idType" id="idType" class="mini-combobox" required="true" onvaluechanged="onIdTypeMouseOut()"
                               textField="codeName" valueField="codeId" emptyText="请选择..." style="width: 230px;"
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_13&pcodeId=-1&sn=1"
                        />
                    </td>
                    <td class="discell"><font color="#dc143c" id="font">*</font>证件号码:</td>
                    <td>
                        <input name="idnumber" id="idnumber" class="mini-textbox" onvaluechanged="onMouseOut()" emptyText="请填写证件号码"
                               style="width: 230px" />
                    </td>
                </tr>
                <tr>
                    <td><input class="mini-hidden" name="num" id="num"></td>
                </tr>
                <tr>
                    <td class="discell">性&emsp;&emsp;别:</td>
                    <td class="input">
                        <input name="gender" id="gender" class="mini-combobox"
                               textField="codeName" valueField="codeId" emptyText="请选择..." style="width: 230px;"
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_2&pcodeId=-1&sn=1"
                        />
                    </td>
                    <td class="discell">出生日期:</td>
                    <td class="input">
                        <input property="editor" class="mini-datepicker" id="age" name="age" allowInput="true" emptyText="请选择出生日期"
                               style="width: 230px" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">工作单位:</td>
                    <td colspan="3">
                        <input name="danwei" id="danwei" class="mini-textbox" emptyText="请输入工作单位" style="width: 96%" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">手机号码:</td>
                    <td class="input">
                        <input name="tel" id="tel" class="mini-textbox" onvaluechanged="checkTel()" emptyText="请输入正确电话" style="width: 230px" />
                    </td>
                    <td class="discell">固定电话:</td>
                    <td class="input">
                        <input name="fixedLine" id="fixedLine" class="mini-textbox" onvaluechanged="checkTel()" emptyText="请输入正确电话"
                               style="width: 230px" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><#--<font color="#dc143c">*</font>-->家庭住址:</td>
                    <td colspan="3" style="width: 800px;">
                        <input id="addrpro" name="addrpro" class="mini-combobox" emptyText="省份" style="width:190px;"
                               textfield="codeName" valueField="codeId"
                               onclick="onDeptChanged('addrpro')" onvaluechanged="onValueChanged('addrpro')" />
                        <input id="addrcity" name="addrcity" class="mini-combobox" emptyText="市" style="width:190px;"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('addrcity')"
                               onvaluechanged="onValueChanged('addrcity')" />
                        <input id="addrcou" name="addrcou" class="mini-combobox" emptyText="县" style="width:190px;"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('addrcou')"
                               onvaluechanged="onValueChanged('addrcou')" />
                        <input id="addrvil" name="addrvil" class="mini-combobox" emptyText="街道" style="width:180px;"
                               textfield="codeName" valueField="codeId"onclick="onDeptChanged('addrvil')" />
                    </td>
                    </td>
                </tr>
                <tr>
                    <td class="discell"></td>
                    <td colspan="3" style="width: 500px;">
                        <input class="mini-textbox" style="width: 760px" emptyText="详细地址" id="addrdetail" name="addrdetail" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"></td>
                    <td colspan="3" style="width: 500px">
                        <font color="#dc143c">*注: 请尽量填写真实家属地址，便于给司法所核查服刑人员信息提供依据</font>
                    </td>
                </tr>
            </table>
        </form>
        <div style="padding:2px;margin: 0 auto;text-align: center">
            <a class="mini-button" iconCls="fa-save" plain="true" style="font-size: 15px" onclick="saveData()">保存</a>
            <a class="mini-button" iconCls="fa-close" plain="true" style="font-size: 15px" onclick="onCancel()">关闭</a>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");
    var id = mini.get("id");
    var tel = mini.get("tel");
    var priNum = mini.get("priNum");
    var addrpro = mini.get("addrpro");
    var addrcity = mini.get("addrcity");
    var addrcou = mini.get("addrcou");
    var addrvil = mini.get("addrvil");
    var idnumber = mini.get("idnumber");
    var idType = mini.get("idType");

    function onMouseOut() {
        var id = mini.get("idnumber").getValue();
        if (idType.getValue() == "1" || idType.getValue() == "2") {
            if (id.length == 18 && onIDCardsValidation(idnumber.getValue())) {
                var age = id.substring(6, 10) + "-" + id.substring(10, 12) + "-" + id.substring(12, 14);
                mini.get("age").setValue(age);
                var num = id[16];
                if(num%2 ==0){
                    mini.get("gender").setValue("2");
                } else {
                    mini.get("gender").setValue("1");
                }
            } else if (id.length == 15 && onIDCardsValidation(idnumber.getValue())) {
                var num = id[14];
                if(num%2 ==0){
                    mini.get("gender").setValue("2");
                } else {
                    mini.get("gender").setValue("1");
                }
            } else {
                mini.alert("请输入正确的身份证号码");
                return false;
            }
        }
        return true;
    }

    // 保存数据
    function saveData() {
        form.validate();
        if (form.isValid() == false) {
            return;
        }
        var idType = mini.get("idType");
        var idNum = mini.get("idnumber");
        if (idNum.getValue() == '' || idNum.getValue() == undefined || idNum.getValue() == null) {
            idType.setValue("98");
        }else {
            idType.setValue("1");
        }
        var data = form.getData();
        var tel = data.fixedLine;
        var phone = data.tel;
        var newPhone = checkTelAndPhone(tel, phone);
        var flag = newPhone['flag'];
        var kflag = newPhone['kflag'];
        if (flag) {
            data.tel = newPhone['newTelAndPhone'];
        } else {
            mini.alert("联系电话或手机号有误，请重填");
            return;
        }
        var idNumberValue = idnumber.getValue();
        if (idNumberValue) {
            if (!onIDCardsValidation(idnumber.getValue())) {
                mini.alert("请输入正确的身份证号码");
                return;
            }
        }
        var json = mini.encode(data);
        $.ajax({
            url: "/azbj/social/srinfo/saveexcel",
            data: {data: json},
            type: "post",
            success: function (success) {
                if (success.code == 1) {
                    mini.alert("保存成功", "信息提示", function () {
                        onCancel();
                    });
                } else if (success.code == 5599) {
                    mini.alert(success.message);
                } else {
                    mini.alert("保存失败");
                }
            }
        });
    }

    var urltemp = "/azbj/getCodeDetail/?codeType=2_1&codeId=-2";
    function onDeptChanged(e) {
        if ("addrpro" == e) {
            var value = addrpro.getValue();
            addrpro.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=-1";
            addrpro.setUrl(url);
            if (value != '') {
                addrpro.setValue(value);
            } else {
                addrpro.select(0);
            }
        }

        if ("addrcity" == e) {
            var value = addrcity.getValue();

            if (addrpro.getValue() == "") {
                addrpro.setUrl(urltemp);
                addrpro.setValue("");
                addrcou.setUrl(urltemp);
                addrcou.setValue("");
                addrvil.setUrl(urltemp);
                addrvil.setValue("");
                return;
            }
            var codeId = addrpro.getValue();
            addrcity.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            addrcity.setUrl(url);
            if (value != '') {
                addrcity.setValue(value);
            } else {
                addrcity.select(0);
            }
        }

        if ("addrcou" == e) {
            var value = addrcou.getValue();
            if (addrcity.getValue() == "") {
                addrcou.setUrl(urltemp);
                addrcou.setValue("");
                addrvil.setUrl(urltemp);
                addrvil.setValue("");
                return;
            }
            var codeId = addrcity.getValue();
            addrcou.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            addrcou.setUrl(url);
            if (value != '') {
                addrcou.setValue(value);
            } else {
                addrcou.select(0);
            }
        }

        if ("addrvil" == e) {
            var value = addrvil.getValue();
            if (addrcou.getValue() == "") {
                addrvil.setUrl(urltemp);
                addrvil.setValue("");
                return;
            }
            var parent_code = addrcou.getValue();
            addrvil.setValue("");
            var url = "/azbj/getCodeDetail?codeType=2_1&codeId=" + parent_code;
            addrvil.setUrl(url);
            if (value != '') {
                addrvil.setValue(value);
            } else {
                addrvil.select(0);
            }
        }
    }

    function onValueChanged(e) {
        if ("addrpro" == e) {
            addrcity.setValue("");
            addrcou.setValue("");
            addrvil.setValue("");
        }
        if ("addrcity" == e) {
            addrcou.setValue("");
            addrvil.setValue("");
        }
        if ("addrcou" == e) {
            addrvil.setValue("");
        }
    }

    function SetData(data) {
        if (data.id) {
            mini.get("id").setEnabled(false);
        }
        $.ajax({
            url: "/azbj/social/srinfoexcel?id=" + data.id,
            type: "post",
            dataType: "json",
            success: function (srdata) {
                var telAndPhone = getTelAndPhone(srdata);
                srdata.tel = telAndPhone['phone'];
                srdata.fixedLine = telAndPhone['tel'];
                var zd = mini.decode(srdata);
                form.setData(srdata);
                form.setChanged(false);
                addrpro.setData([{codeId: srdata.addrpro, codeName: srdata.addrproname}]);
                addrcity.setData([{codeId: srdata.addrcity, codeName: srdata.addrcityname}]);
                addrcou.setData([{codeId: srdata.addrcou, codeName: srdata.addrcouname}]);
                addrvil.setData([{codeId: srdata.addrvil, codeName: srdata.addrvilname}]);
            }
        });
    }

    function getTelAndPhone(o) {
        var telAndPhone = o.tel;
        if (telAndPhone.includes("|")) {
            var tel = telAndPhone.substring(0, telAndPhone.indexOf("|"));
            var phone = telAndPhone.substring(telAndPhone.indexOf("|") + 1, telAndPhone.length);
        } else {
            if ((/^1[3456789]\d{9}$/.test(telAndPhone))) {
                var tel = "";
                var phone = telAndPhone;
            } else {
                var tel = telAndPhone;
                var phone = "";
            }
        }
        var telAndPhoneBack = {'tel': tel, 'phone': phone};
        return telAndPhoneBack;
    }

    function checkTel(e) {
        var phone = mini.get("tel").getValue();
        var fixedLine = mini.get("fixedLine").getValue();
        if (phone == '' && fixedLine == '') {
            mini.alert("请至少输入一个联系方式");
            return false;
        }
        if (phone != '' && !(/^1[3456789]\d{9}$/.test(phone))) {
            mini.alert("手机号码有误，请重填");
            return false;
        }
        if (fixedLine != '' && !/^((0\d{2,3})-?)(\d{7,8})$/.test(fixedLine)) {
            mini.alert('固定电话有误，请重填');
            return false;
        }
    }

    window.onload = function () {
        priNum.setValue(${priNum});
    }

    function onIdTypeMouseOut() {
        var idType = mini.get("idType");
        var idNum = mini.get("idnumber");
        var num = mini.get("num");
        idNum.setEnabled(true);
        idNum.setEmptyText("请填写证件号码");
        //如果证件类型选择'无'
        if (idType.getValue() === '98') {
            idNum.setEmptyText("无证件号码，不用填写");
            $("#font").hide();
            idNum.required = false;
            $("#idNum").toggleClass("mini-textbox-disabled mini-textbox-readOnly");
            idNum.setEnabled(false);
            num.setValue(idNum.getValue());
            idNum.setValue('无');
        } else {
            //如果证件类型选择其他
            if (idNum.getValue() === '' && (num.getValue() !== '' || num.getValue() !== null)) {
                idNum.setValue(num.getValue());
            }
            idNum.setEmptyText("请填写证件号码");
            idNum.setEnabled(true);
            $("#font").show();
            idNum.required = true;
        }
        var id = mini.get("idnumber").getValue();
        if (idType.getValue() == "1" || idType.getValue() == "2") { //当证件类型为身份证号或者临时身份证号时才进行校验
            if (onIDCardsValidation(id)) {
                var age = id.substring(6, 10) + "-" + id.substring(10, 12) + "-" + id.substring(12, 14);
                mini.get("age").setValue(age);
                var num = id[16];
                if(num%2 ==0){
                    mini.get("gender").setValue("2");
                } else {
                    mini.get("gender").setValue("1");
                }
            }else {
                mini.alert("请输入正确的身份证号码");
            }

        }
    }
</script>
</body>
</html>